﻿using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace bonus.Controllers
{
	public class IndexController : Controller
	{
		// GET: 首页
		public ActionResult Index()
		{
			Repository.Repository tool = new Repository.Repository();

			var user = tool.GetOne<Models.User>(Services.Cookie.GetId());

			if (user.UserType == Models.User.TypeClass.User)
			{
				var filtern = Builders<Models.Notice>.Filter.Where(p => p.IsUser);

				var notice = tool.GetMany<Models.Notice>(filtern, "AddDT:-1", 3, 1);

				ViewBag.Notice = notice;
			}
			else if (user.UserType == Models.User.TypeClass.COMPANY)
			{
				var filtern = Builders<Models.Notice>.Filter.Where(p => p.IsCampay);

				var notice = tool.GetMany<Models.Notice>(filtern, "AddDT:-1", 3, 1);

				ViewBag.Notice = notice;
			}
			else if (user.UserType == Models.User.TypeClass.CONTENT)
			{
				var filtern = Builders<Models.Notice>.Filter.Where(p => p.IsCenter);

				var notice = tool.GetMany<Models.Notice>(filtern, "AddDT:-1", 3, 1);

				ViewBag.Notice = notice;
			}
			else
			{
				var notice = tool.GetMany<Models.Notice>(new BsonDocument(), "AddDT:-1", 3, 1);

				ViewBag.Notice = notice;
			}


			var filter = Builders<Models.Message>.Filter.Where(p => p.UserNo == user.NO);

			ViewBag.Message = tool.GetMany(filter, "AddDT:-1");


			if (user.UserType == Models.User.TypeClass.ADMIN)
			{
				var filtere = Builders<Models.Journal>.Filter.Where(p => p.State == 0 && p.Type == Models.Journal.TypeClass.WITHDRAW);

				var count = tool.Count(filtere);

				var filters = Builders<Models.Message>.Filter.Where(p => string.IsNullOrEmpty(p.Content));

				var list = tool.Count(filters);

				HttpCookie cookie = new HttpCookie(Models.WebConfig.MSGCOOKIE);

				if (Request.Cookies[Models.WebConfig.MSGCOOKIE] != null)
				{
					HttpCookie _cookie = Request.Cookies[Models.WebConfig.MSGCOOKIE];
					_cookie.Expires = DateTime.Now.AddHours(-1);
					Response.Cookies.Add(_cookie);
				}

				if (list == 0 && count == 0)
				{
					cookie.Values.Add("Code", "0");
					cookie.Values.Add("Account", "0");
					cookie.Values.Add("Withdraw", "0");

					cookie.Expires = DateTime.Now.AddDays(1);

					byte[] byteArray = Encoding.Default.GetBytes(cookie.Value);

					cookie.Value = HttpUtility.UrlEncode(byteArray).ToString();

					System.Web.HttpContext.Current.Response.AppendCookie(cookie);

				}
				else
				{
					cookie.Values.Add("Code", "1");
					cookie.Values.Add("Account", list.ToString());
					cookie.Values.Add("Withdraw", count.ToString());
					cookie.Expires = DateTime.Now.AddDays(1);

					byte[] byteArray1 = Encoding.Default.GetBytes(cookie.Value);

					cookie.Value = HttpUtility.UrlEncode(byteArray1).ToString();

					System.Web.HttpContext.Current.Response.AppendCookie(cookie);
				}

			}

			return View(user);
		}

		/// <summary>
		/// 折现图返回数据
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public JsonResult GetLineData()
		{
			Repository.Repository tool = new Repository.Repository();

			var date = DateTime.Now.AddMonths(-6);

			string[] strarr = new string[6];

			int[] intarr = new int[6];

			for (int i = 0; i < 6; i++)
			{
				var datet = date.AddMonths(i);
				var datestr = datet.Year.ToString("") + "," + datet.Month;

				var filterM = Builders<Models.Monthl>.Filter.Where(p => p.DataDT == datestr && p.Type == 5);

				var month = tool.GetOne(filterM);

				strarr[i] = datet.ToString("yy") + "年" + datet.Month + "月";

				if (month == null)
				{

					intarr[i] = 0;
				}
				else
				{
					intarr[i] = Convert.ToInt32(month.Money);
				}
			}

			return Json(new { Code = "OK", Labels = strarr, Data = intarr });
		}

		/// <summary>
		/// 获取月数报单所有数据
		/// </summary>
		/// <param name="size"></param>
		/// <param name="index"></param>
		/// <returns></returns>
		[HttpPost]
		public JsonResult GetMonthData(int size = 10, int index = 1)
		{
			Repository.Repository tool = new Repository.Repository();

			var filter = Builders<Models.Monthl>.Filter.Where(p => p.Type == 5);

			var count = tool.Count<Models.Monthl>(filter);

			var list = tool.GetMany<Models.Monthl>(filter, "", size, index);

			ICollection<object> Entities = new List<object>() { };

			foreach (var item in list)
			{
				object i = new
				{
					Id = item.DataDT,
					Type = "报单",//申请者编号
					DataStr = item.DataStr,
					Money = item.Money.ToString("0.##"),
					AddDT = item.AddDT,
				};
				Entities.Add(i);
			}


			return Json(new { Code = "OK", Data = Entities, total = count });//Totle:总条数  Count:总页数   Index 当前页
		}

		/// <summary>
		/// 饼状图数据
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public JsonResult GetPie()
		{
			Repository.Repository tool = new Repository.Repository();
			var user = tool.GetOne<Models.User>(Services.Cookie.GetId());
			if (user == null)
			{
				return Json(new { Code = "FAIL", Msg = "用户不存在" });
			}

			if (user.UserType == Models.User.TypeClass.ADMIN)
			{
				var date = DateTime.Now.AddMonths(-1);
				var datestr = date.Year.ToString("") + "," + date.Month;
				var filter = Builders<Models.Monthl>.Filter.Where(p => p.DataDT == datestr);
				var list = tool.GetMany(filter);

				int[] intarra = { 0, 1, 2, 3, 4 };

				int[] intarrb = new int[5];

				string[] strarr = new string[5];

				for (int i = 0; i < intarra.Count(); i++)
				{
					intarrb[i] = Convert.ToInt32(list.FindAll(p => p.Type == intarra[i]).FirstOrDefault() == null ? 0 : list.FindAll(p => p.Type == intarra[i]).FirstOrDefault().Money);
					strarr[i] = Models.Journal.TypeClass.GetType(intarra[i]);
				}

				return Json(new { Code = "OK", Msg = date.Year.ToString("") + "年" + date.Month + "月数据", Lables = strarr, Data = intarrb });
			}
			else
			{
				var date = DateTime.Now.AddMonths(-1);
				var datestr = date.Year.ToString("") + "," + date.Month;
				var filter = Builders<Models.Journal>.Filter.Where(p => p.GetNO == user.NO);
				var list = tool.GetMany(filter);

				int[] intarra = { 0, 1, 2, 3, 4 };

				int[] intarrb = new int[5];

				string[] strarr = new string[5];

				for (int i = 0; i < intarra.Count(); i++)
				{
					var temp = list.FindAll(p => p.Type == intarra[i]);

					decimal money = 0;

					foreach (var item in temp)
					{
						money += item.In;
					}

					intarrb[i] = Convert.ToInt32(money);
					strarr[i] = Models.Journal.TypeClass.GetType(intarra[i]);
				}

				return Json(new { Code = "OK", Msg = "财务数据", Lables = strarr, Data = intarrb });
			}
		}


		/// <summary>
		/// 获取一个月的数据
		/// </summary>
		/// <param name="month">月份</param>
		/// <param name="year">年份</param>
		/// <returns></returns>
		[HttpPost]
		public JsonResult GetOneMonth(int year, int month)
		{
			Repository.Repository tool = new Repository.Repository();
			var date = new DateTime(year, month, 1);

			var filter = Builders<Models.Order>.Filter.Where(p => p.AddDT >= date && p.AddDT < date.AddMonths(1));
			var list = tool.GetMany(filter);

			var day = DateTime.DaysInMonth(year, month);

			string[] strarr = new string[day];
			int[] intarr = new int[day];

			for (int i = 0; i < day; i++)
			{
				decimal money = 0;
				foreach (var item in list.FindAll(p => p.AddDT >= date.AddDays(i) && p.AddDT < date.AddDays(i + 1)))
				{
					money += item.Money;
				}

				strarr[i] = (i + 1).ToString() + "号";
				intarr[i] = Convert.ToInt32(money);
			}
			return Json(new { Code = "OK", Msg = month, Labels = strarr, Data = intarr });
		}


		/// <summary>
		/// 提现申请
		/// </summary>
		/// <returns></returns>
		public JsonResult Withdraws(int size = 10, int index = 1)
		{
			Repository.Repository tool = new Repository.Repository();

			var user = tool.GetOne<Models.User>(Services.Cookie.GetId());

			if (user == null || user.UserType != Models.User.TypeClass.ADMIN)
			{
				return Json(new { Code = "FAIL", Msg = "用户权限不够或未登陆" });
			}

			var filter = Builders<Models.Journal>.Filter.Where(p => p.State == 0 && p.Type == Models.Journal.TypeClass.WITHDRAW);

			var count = tool.Count(filter);

			var list = tool.GetMany(filter, "AddDT:-1", size, index);
			ICollection<object> Entities = new List<object>() { };
			foreach (var item in list)
			{
				object i = new
				{
					Id = item._id.ToString(),
					GetNo = item.GetNO,//申请者编号
					AddDT = item.AddDT.ToString("yyyy-MM-dd HH:mm"),
					Money = item.Out.ToString("0.##"),
					Name = item.Name,
					JournalNO = string.IsNullOrEmpty(item.JournalNO) ? "无" : item.JournalNO,
				};
				Entities.Add(i);

			}
			return Json(new { Code = "OK", Msg = "查询成功", Data = Entities, total = count });
		}

		/// <summary>
		/// 获取要显示的账户申请记录和提现申请
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public void Message()
		{
			Repository.Repository tool = new Repository.Repository();

		}

	}
}